package com.zhugang.week04;

/**
 * @program algorithms
 * @description: mergeArray
 * @author: chanzhugang
 * @create: 2022/06/29 17:25
 */
public class MergeArray {

    /**
     * 面试题 10.01. 合并排序的数组
     *
     * @param A
     * @param m
     * @param B
     * @param n
     */
    public void merge(int[] A, int m, int[] B, int n) {
        // B往A合
        int i = m - 1;
        int j = n - 1;
        int k = m + n - 1;
        while (i >= 0 && j >= 0) {
            if (A[i] >= B[j]) {
                A[k--] = A[i--];
            } else {
                A[k--] = B[j--];
            }
        }
        while (i >= 0) {
            A[k--] = A[i--];
        }
        while (j >= 0) {
            A[k--] = B[j--];
        }
    }
}